import { getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { useCallback, useMemo, useState } from 'react';
import { PlanManageAPI } from '../common/type';
import { ManageSearchColumns, ManageTabSwitch } from '../mconfig';
import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
// import { ManageSearchColumns } from '../mconfig';
// import { disposeArea } from '../common';

const baseUrl = getBaseUrl();

const PlanManageServices = (curRes: Record<string, any>) => {
  /**
   * 获取数据字典
   * */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`,
    // `${getTenantID()}${ILoadMapTypeAll.PROCURE_CANCELTYPE}`,
  ];

  /***获取数据字典loadMdicMap**/
  const { loadMapInfo, mapLoading } = getLoadMapList(loadMdicIds);

  /**
   * 保存当前的搜索参数
   * */
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: PlanManageAPI.IAssetTableRequestType) => {
      /**
       * ======演示注释======
       * 【演示写死createCompanyID】
       * */
      let reqUrl = `${baseUrl}/purchasePlanController/dataGrid/${reqParams?.params}`; //【真实】
      // let reqUrl = `${baseUrl}/purchasePlanController/dataGrid/createCompanyID`;//【演示】

      let PostParams = {
        current: 1,
        pageSize: 10,
        dicTypeIds: loadMdicIds?.toString(),
        sort: 'createTime',
        order: 'desc',
        // state: '0',
        ...reqParams,
      };

      /***保存一份参数**/
      setManageTableParams(PostParams);

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: PlanManageAPI.IAssetTableRequestType | any) => {
    if (reqParams?.createTime) {
      const [createTimeStart, createTimeEnd] = reqParams.createTime;
      reqParams['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['createTime'];
    }

    if (reqParams?.applyTime) {
      const [applyTimeStart, applyTimeEnd] = reqParams.applyTime;
      reqParams['applyTimeStart'] = applyTimeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['applyTimeEnd'] = applyTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['applyTime'];
    }

    if (reqParams?.cancelTime) {
      const [cancelTimeStart, cancelTimeEnd] = reqParams.cancelTime;
      reqParams['cancelTimeStart'] = cancelTimeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['cancelTimeEnd'] = cancelTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['cancelTime'];
    }

    if (reqParams?.ownTime) {
      const [ownTimeStart, ownTimeEnd] = reqParams.ownTime;
      reqParams['ownTimeStart'] = ownTimeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['ownTimeEnd'] = ownTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['ownTime'];
    }

    delete reqParams['createFormNameDept'];
    delete reqParams['createFormNameUser'];
    if (reqParams?.createrID && reqParams?.createrID?.length > 0) {
      reqParams['createrID'] = reqParams?.createrID?.toString();
    } else {
      delete reqParams['createrID'];
    }

    run({
      ...reqParams,
      ...ManageTabSwitch(curRes),
    });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 搜索功能配置columns
   * */
  const searchList = useMemo(() => {
    return {
      loadMapInfo,
      searchList: ManageSearchColumns(loadMapInfo),
    };
  }, [loadMapInfo, manageTableParams]);

  return {
    dataRow: data,
    loading,
    reloadRequest,
    manageTableParams,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

export { PlanManageServices };
